import * as api from '../../api/common';

Page({
  data: {
    showVerification: false,
    phoneNumber: '',
    verificationCode: '',
    countdown: 0,
    timer: null,
    checked: false,
    loading: false
  },
  toRegister(){
    tt.navigateTo({
      url: '../register/index',
      success: (res) => {
        
      },
      fail: (res) => {
        
      },
    });
  },
  onLoad(options) {
    // 页面加载时的初始化工作，如果有需要可以在这里处理
  },
  gotoPrivacyPage() {
    tt.navigateTo({
      url: 'pages/privacy/index'
    });
  },

  gotoServicePage() {
    tt.navigateTo({
      url: 'pages/service/index'
    });
  },
  onUnload() {
    // 页面卸载时清除定时器，避免内存泄漏
    if (this.data.timer) {
      clearInterval(this.data.timer);
    }
  },

  // 去账号密码登录页面
  loginByAccount() {
    tt.redirectTo({
      url: '../loginByAccount/index'
    });
  },

  // 同意/取消用户协议
  toggleCheckbox() {
    this.setData({
      checked: !this.data.checked
    });
  },

  // 输入手机号
  inputPhone(e) {
    this.setData({
      phoneNumber: e.detail.value
    });
  },

  // 校验手机号格式
  checkPhoneNumber(phoneNumber) {
    const reg = /^1\d{10}$/;
    return reg.test(phoneNumber);
  },

  // 发送验证码
  async sendVerificationCode() {
    if (this.data.loading) {
      return
    }
    this.setData({
      loading: true
    })
    const phoneNumber = this.data.phoneNumber.trim();

    if (!this.checkPhoneNumber(phoneNumber)) {
      tt.showToast({
        title: '请输入正确的手机号码',
        icon: 'none'
      });
      this.setData({
        loading: false
      })
      return;
    }

    try {
      const response = await api.sendVerificationCode({
        phone: phoneNumber
      });
      tt.showToast({
        title: '验证码发送成功',
        icon: 'success'
      });
      if (response.code === 0) {
        tt.showToast({
          title: response.msg,
          icon: 'none'
        });
      } else {
        this.setData({
          showVerification: true
        });
        this.startCountdown();
      }
    } catch (error) {
      tt.showToast({
        title: error,
        icon: 'none'
      });
    } finally {
      setTimeout(() => {
        this.setData({
          loading: false
        })
      }, 500)
    }
  },

  // 输入验证码
  inputVerificationCode(e) {
    this.setData({
      verificationCode: e.detail.value
    });
  },

  // 开始倒计时
  startCountdown() {
    let count = 60;
    this.setData({
      countdown: count
    });
    this.data.timer = setInterval(() => {
      count--;
      if (count >= 0) {
        this.setData({
          countdown: count
        });
      } else {
        clearInterval(this.data.timer);
        this.setData({
          countdown: 0
        });
      }
    }, 1000);
  },
  // 勾选
  toggleCheckbox() {
    this.setData({
      checked: !this.data.checked // 切换勾选状态
    });
  },
  // 提交验证码登录
  async submit() {
    if (this.data.loading) {
      return
    }
    this.setData({
      loading: true
    })
    
    const phoneNumber = this.data.phoneNumber.trim();
    const verificationCode = this.data.verificationCode.trim();

    if (!this.checkPhoneNumber(phoneNumber)) {
      tt.showToast({
        title: '请输入有效的手机号',
        icon: 'none'
      });
      this.setData({
        loading: false
      })
      return;
    }

    if (!verificationCode) {
      tt.showToast({
        title: '请输入验证码',
        icon: 'none'
      });
      this.setData({
        loading: false
      })
      return;
    }

    if (!this.data.checked) {
      tt.showToast({
        title: '请阅读并同意用户协议和隐私权条款',
        icon: 'none'
      });
      this.setData({
        loading: false
      })
      return;
    }

    try {
      const response = await api.login({
        type: 'code',
        phone: phoneNumber,
        code: verificationCode
      });

      if (response.code === 0) {
        tt.showToast({
          title: response.msg,
          icon: 'none'
        });
      } else {
        // 登录成功处理
        tt.setStorageSync('info', response.data);
        // 跳转首页
        tt.redirectTo ({
          url: '../index/index'
        });
      }
    } catch (error) {
      tt.showToast({
        title: error,
        icon: 'none'
      });
    } finally {
      setTimeout(() => {
        this.setData({
          loading: false
        })
      }, 500)
    }
  }
});